import { Injectable,Inject } from '@angular/core';
import {HttpClient,HttpHeaders} from "@angular/common/http";
import {Project, User} from "../domain"
import {Observable,from} from "rxjs";
import * as _ from "lodash";
import {map,mergeMap,count,switchMap,mapTo} from "rxjs/operators";
@Injectable({
  providedIn: 'root'
})
export class ProjectService {
  private readonly domain = 'projects';
  private headers = new HttpHeaders({
    'content-Type':'application/json'
  })
  constructor(private http:HttpClient,@Inject('BASE_CONFIG') private config) { }
  //post
  add(project:Project):Observable<Project>{
    project.id = null;
    const uri = `${this.config.uri}/${this.domain}`;
    return this.http.post(uri,JSON.stringify(project),{headers:this.headers}).pipe(
      map(res=>res as Project)
    )
  }
  //post
  update(project:Project):Observable<Project>{
    const uri = `${this.config.uri}/${this.domain}/${project.id}`;
    const toUpdate = {
      name:project.name,
      desc:project.desc,
      coverImg:project.coverImg
    };
    return this.http.patch(uri,JSON.stringify(toUpdate),{headers:this.headers}).pipe(
      map(res=>res as Project)
    )
  }
  //delete
  del(project:Project):Observable<Project>{
    const delTasks$ = from(project.taskLists?project.taskLists:[]).pipe(
      mergeMap(listId=>this.http.delete(`${this.config.uri}/taskLists/${listId}`)),
      count()
    );
    return delTasks$.pipe(
      switchMap(_=>this.http.delete(`${this.config.uri}/${this.domain}/${project.id}`)),
      map(_=>project)

      );
  }
  //get
  getProjects(userId:string):Observable<Project>{
    const uri = `${this.config.uri}/${this.domain}`;
    return this.http.get(uri,{params:{'members_like':userId}}).pipe(
      map(res => res as Project)
    )
  }
  // invite
  invite(projectId:string,users: User[]):Observable<Project>{
    const uri = `${this.config.uri}/${this.domain}/${projectId}`;
    // return this.http.get(uri).pipe(
    //   map(res=>res),
    //   switchMap(project=>{
    //     const existingMembers = project.members;
    //     const invitedIds = users.map(user=>user.id);
    //     const newIds  = _.union(existingMembers,invitedIds);
    //     return this.http
    //     .patch(uri,JSON.stringify({members:newIds}),{headers:this.headers}))
    //     .pipe(
    //       map(res => res)
    //     )
    //   }
    // )
    return this.http.get(uri).pipe(
      map(res => res),
      switchMap((project: Project) => {
        const existingMembers = project.members;
        const invitedIds = users.map(user=>user.id);
        const newIds  = _.union(existingMembers,invitedIds);
        return this.http.patch(uri, JSON.stringify({members:newIds}),{headers:this.headers})
      }),
      map(res => res as Project)
    );
  }
}
